package com.ruoyi.web.controller.customermerchants;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.RateLimiter;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.repairs.domain.CrmRepairs;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.customermerchants.domain.CrmCustomerMerchants;
import com.ruoyi.customermerchants.service.ICrmCustomerMerchantsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户线索Controller
 * 
 * @author hct
 * @date 2022-02-16
 */
@RestController
@RequestMapping("/customermerchants/customermerchants")
public class CrmCustomerMerchantsController extends BaseController
{
    @Autowired
    private ICrmCustomerMerchantsService crmCustomerMerchantsService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询客户线索列表
     */
    @PreAuthorize("@ss.hasPermi('customermerchants:customermerchants:list')")
    @GetMapping("/list")
    public TableDataInfo list(CrmCustomerMerchants crmCustomerMerchants)
    {
        startPage();
        List<CrmCustomerMerchants> list = crmCustomerMerchantsService.selectCrmCustomerMerchantsList(crmCustomerMerchants);
        return getDataTable(list);
    }

    /**
     * 查询分配的客户线索列表
     */
    @PreAuthorize("@ss.hasPermi('customermerchants:customermerchants:task')")
    @GetMapping("/task")
    public TableDataInfo task(CrmCustomerMerchants crmCustomerMerchants)
    {   crmCustomerMerchants.setStatus(0);
        crmCustomerMerchants.setUserId(0L);
        startPage();
        List<CrmCustomerMerchants> list = crmCustomerMerchantsService.selectCrmCustomerMerchantsListN(crmCustomerMerchants);
        return getDataTable(list);
    }

    /**
     * 查询我的客户线索列表
     */
    @PreAuthorize("@ss.hasPermi('customermerchants:customermerchants:my')")
    @GetMapping("/my")
    public TableDataInfo my(CrmCustomerMerchants crmCustomerMerchants)
    {
        crmCustomerMerchants.setUserId(getUserId());
        startPage();
        List<CrmCustomerMerchants> list = crmCustomerMerchantsService.selectCrmCustomerMerchantsList(crmCustomerMerchants);
        return getDataTable(list);
    }

    /**
     * 导出客户线索列表
     */
    @PreAuthorize("@ss.hasPermi('customermerchants:customermerchants:export')")
    @Log(title = "客户线索", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrmCustomerMerchants crmCustomerMerchants)
    {
        List<CrmCustomerMerchants> list = crmCustomerMerchantsService.selectCrmCustomerMerchantsList(crmCustomerMerchants);
        ExcelUtil<CrmCustomerMerchants> util = new ExcelUtil<CrmCustomerMerchants>(CrmCustomerMerchants.class);
        util.exportExcel(response, list, "客户线索数据");
    }

    /**
     * 获取客户线索详细信息
     */
    @PreAuthorize("@ss.hasPermi('customermerchants:customermerchants:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        CrmCustomerMerchants crmCustomerMerchants=crmCustomerMerchantsService.selectCrmCustomerMerchantsById(id);
        if(crmCustomerMerchants!=null && crmCustomerMerchants.getUserId()!=null){
            SysUser sysUser=sysUserService.selectUserById(crmCustomerMerchants.getUserId());
            crmCustomerMerchants.setSysUser(sysUser);
        }
         return AjaxResult.success(crmCustomerMerchants);
    }

    /**
     * 新增客户线索
     */
    @PreAuthorize("@ss.hasPermi('customermerchants:customermerchants:add')")
    @Log(title = "客户线索", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrmCustomerMerchants crmCustomerMerchants)
    {
        return toAjax(crmCustomerMerchantsService.insertCrmCustomerMerchants(crmCustomerMerchants));
    }

    /**
     * 修改客户线索
     */
    @PreAuthorize("@ss.hasPermi('customermerchants:customermerchants:edit')")
    @Log(title = "客户线索", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrmCustomerMerchants crmCustomerMerchants)
    {
        return toAjax(crmCustomerMerchantsService.updateCrmCustomerMerchants(crmCustomerMerchants));
    }

    /**
     * 删除客户线索
     */
    @PreAuthorize("@ss.hasPermi('customermerchants:customermerchants:remove')")
    @Log(title = "客户线索", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(crmCustomerMerchantsService.deleteCrmCustomerMerchantsByIds(ids));
    }


    /**
     * 修改人
     */
    @PreAuthorize("@ss.hasPermi('customermerchants:customermerchants:review')")
    @Log(title = "修改人", businessType = BusinessType.UPDATE)
    @PostMapping("/review")
    public AjaxResult review(@RequestBody CrmCustomerMerchants crmCustomerMerchants)
    {
        CrmCustomerMerchants crmCustomerMerchantsN=new CrmCustomerMerchants();
        crmCustomerMerchantsN.setId(crmCustomerMerchants.getId());
        crmCustomerMerchantsN.setUserId(crmCustomerMerchants.getUserId());
        return toAjax(crmCustomerMerchantsService.updateCrmCustomerMerchants(crmCustomerMerchantsN));
    }

    /**
     * 转换为客户
     */
    @RateLimiter(time = 1,count = 1)
    @PreAuthorize("@ss.hasPermi('customermerchants:customermerchants:tocustomer')")
    @Log(title = "修改人", businessType = BusinessType.UPDATE)
    @PostMapping("/tocustomer/{id}")
    public AjaxResult merchantsToCustomer(@PathVariable Long id)
    {
        return toAjax(crmCustomerMerchantsService.merchantsToCustomer(id));
    }
}
